//
//  ACMineLogViewController.m
//  ACLiteAVUISDK
//
//  Created by Ben on 2021/4/14.
//

#import "ACMineLogViewController.h"
#import "ACMineLogCell.h"
#import "ACFileInfoViewController.h"
#import "ACLiveModel.h"

@interface ACMineLogViewController () <UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UILabel *countLabel;
@property(nonatomic,strong) UILabel *numberLabel;
@property(nonatomic,strong) UITableView *tableview;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,assign)int page;
@property(nonatomic,assign)int pageCount;
@property(nonatomic,assign)int fileTotal;
@end

@implementation ACMineLogViewController

-(UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _countLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _countLabel.textColor = UIColorFromRGB(0x303133);
    }
    return _countLabel;
}

-(UILabel *)numberLabel{
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _numberLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _numberLabel.textColor = UIColorFromRGB(0xFF6048);
    }
    return _numberLabel;
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.rowHeight = 108;
        _tableview.backgroundColor = UIColorFromRGB(0xFFFFFF);
        _tableview.tableFooterView = [UIView new];
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACMineLogCell" bundle:curB] forCellReuseIdentifier:@"ACMineLogCell"];
    }
    return _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorFromRGB(0xF4F5F8);
    
    UIView *countView = [[UIView alloc] initWithFrame:CGRectZero];
    countView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:countView];
    [countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.top.equalTo(self.view).with.offset(10);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    
    [countView addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(countView).with.offset(15);
        make.centerY.mas_equalTo(0);
    }];
    
    
    [countView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.countLabel.mas_right).with.offset(5);
        make.centerY.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(61);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.bottom.equalTo(self.view).with.offset(0);
    }];
    @weakify(self);
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self requestData];
    }];
    
    self.dataArray = [NSMutableArray array];
    NSString *title = @"";
    NSString *countStr = @"";
    switch (self.type) {
        case MineLogType_Live:
            title = @"我的直播记录";
            countStr = @"直播数量:";
            break;
        case MineLogType_Meeting:
            title = @"我的会议记录";
            countStr = @"会议数量:";
            break;
        case MineLogType_Vod:
            title = @"我的点播";
            countStr = @"点播数量:";
            break;
        case MineLogType_File:
            title = @"我的素材库";
            countStr = @"素材数量:";
            [self getFiles];
            break;
    }
    self.title = title;
    self.countLabel.text = countStr;
    
    self.pageCount = 10;
    self.page = 1;
    [self.tableview.mj_header beginRefreshing];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.type == MineLogType_File) {
        return self.dataArray.count;;
    }
    return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACMineLogCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACMineLogCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ACMineLogCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACMineLogCell"];
    }
    if (self.type == MineLogType_File) {
        ACFileModel *model = self.dataArray[indexPath.row];
        [cell setFileModel:model];
    }else{
        cell.logImg.image = [UIImage imageWithName:@"live_placeHolder" bundle:ACLiteAVUISDK targetClass:[self class]];
        cell.typeImg.image = [UIImage imageWithName:@"mine_live" bundle:ACLiteAVUISDK targetClass:[self class]];
        cell.nameLabel.text = @"文件名";
        cell.liveTimeLabel.text = @"直播时间:";
        cell.uploadTimeLabel.text = @"累计观看人数:";
    }
    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == MineLogType_File) {
        ACFileModel *fileModel = self.dataArray[indexPath.row];
        ACFileInfoViewController *fileInfo = [[ACFileInfoViewController alloc] init];
        fileInfo.id_file = fileModel.id_file;
        [self.navigationController pushViewController:fileInfo animated:YES];
    }
}

//侧滑操作
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == MineLogType_File) {
        return YES;
    }
    return NO;
}

//选择删除样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

//删除的回调
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (self.type == MineLogType_File) {
            [self deleteFile:indexPath];
        }
    }
}

//修改编辑按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

//刷新数据
-(void)requestData{
    if (self.type == MineLogType_File) {
        [self getFiles];
    }else{
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
    }
}

-(void)addMJFoot{
    @weakify(self);
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.page += 1;
        [self requestData];
    }];
}

-(void)setNoMoreData{
    [self.tableview.mj_footer endRefreshingWithNoMoreData];
}

//请求文件列表
-(void)getFiles{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/material/material"];
    //self 0查询全部\1查询自己的
    NSDictionary *params = @{@"page_size":[NSString stringWithFormat:@"%d",self.pageCount],@"page":[NSString stringWithFormat:@"%d",self.page],@"self":@"1"};
    [[ACNetWorkTool shareRequestManager] get:url params:params success:^(id  _Nonnull responseObject) {
        self.fileTotal = [[responseObject objectForKey:@"total"] intValue];
        self.numberLabel.text = [NSString stringWithFormat:@"%d条",self.fileTotal];
        NSArray *dataArr = [responseObject valueForKey:@"data"];
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
            self.dataArray = [ACFileModel mj_objectArrayWithKeyValuesArray:dataArr];
            if (dataArr.count >= self.pageCount) {
                [self addMJFoot];
            }else{
                [self setNoMoreData];
            }
        }else{
            [self.dataArray addObjectsFromArray:[ACFileModel mj_objectArrayWithKeyValuesArray:dataArr]];
            
            if (dataArr.count < self.pageCount) {
                [self setNoMoreData];
            }else{
                [self.tableview.mj_footer endRefreshing];
            }
        }
        [self.tableview.mj_header endRefreshing];
        [self.tableview reloadData];
        } failure:^(NSError * _Nonnull error) {
            [self.tableview.mj_header endRefreshing];
            [self.tableview.mj_footer endRefreshing];
            [SVProgressHUD showInfoWithStatus:@"网络错误"];
    }];
}

-(void)deleteFile:(NSIndexPath *)path{
    ACFileModel *model = self.dataArray[path.row];
    //判断是否是自己的文件
    if ([[[ACUserProfileManager shareInstance] getUserModel].user_id isEqual:model.created_by]) {
        [SVProgressHUD showWithStatus:@""];
        NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/material/material/"];
        url = [url stringByAppendingString:model.id_file];

        [[ACNetWorkTool shareRequestManager] del:url params:[NSDictionary dictionary] success:^(id  _Nonnull responseObject) {
                [self.dataArray removeObject:model];
                [self.tableview reloadData];
                self.fileTotal -= 1;
                self.numberLabel.text = [NSString stringWithFormat:@"%d条",self.fileTotal];
                [SVProgressHUD dismiss];
            } failure:^(NSError * _Nonnull error) {
                [SVProgressHUD showInfoWithStatus:@"网络错误"];
            }];
    }else{
        [SVProgressHUD showInfoWithStatus:@"不是私有文件,无权限删除"];
    }
    
}


@end
